import { getUserInfo, login } from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'

const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = null
    removeToken()
  },
  setUserInfo(state, userInfo) {
    state.userInfo = userInfo
  },
  removeUserInfo(state) {
    state.userInfo = {}
  }
}
const actions = {
  /* login({ commit }, obj) {
    setTimeout(() => {
      commit('setToken', '123')
    }, 3000)
  } */
  logout({ commit }) {
    commit('removeToken')
    commit('removeUserInfo')
  },
  async login({ commit }, data) {
    const token = await login(data)
    console.log(token)
    commit('setToken', token)
    // context.commit('setToken', token)
  },
  async getUserInfo({ commit }) {
    /* setTimeout(() => {
      commit('setUserInfo', { name: '张三', age: 18 })
    }, 3000) */
    const res = await getUserInfo()
    commit('setUserInfo', res)
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
